/* 
 * @author wxj
 * @version 1.4
 * ���ܣ��û�����
 * 2006-12-27
 */

package com.zjht.gmcc.servlet.sys;

import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;
import com.zjht.gmcc.bean.util.MyJDBC;


public class AddDepartment extends BaseServlet {
	private static final long serialVersionUID = 1L;

	public boolean process(HttpServletRequest request, HttpServletResponse response,HttpSession session, PrintWriter out, MyJDBC myJDBC,  String menu)throws Exception {
		int i_flag = 0;

		// ===============
		String s_zzlb = request.getParameter("zzlbid");
		// String flag = request.getParameter("flag");
		String s_addfs = request.getParameter("addfs"); // 添加方式
		String orgatypeid = request.getParameter("orgatypeid");
		// String url ="/sys/orga/departmentadd.jsp?zzlb="+s_zzlb;
		/*
		 * String s_destination_cg
		 * ="/sys/depart/departmentadd.jsp?zzlb="+s_zzlb+"&flag="+flag+"&orgatypeid="+orgatypeid; String
		 * s_destination_top ="/sys/depart/depart.jsp?zzlb="+s_zzlb+"&flag="+flag+"&orgatypeid="+orgatypeid;
		 */

		String s_sql = null;
		PreparedStatement ps = null;
		PreparedStatement ps2 = null;
		ResultSet rs = null;

		String parentdeptid = request.getParameter("parentdeptid");// 父部门节点
		String s_bm = "";
		String s_gw = "";
		if ("1".equals(s_addfs)) {// 添加岗位
			s_bm = request.getParameter("bmid1");
			s_gw = request.getParameter("gw1");
		} else {
			s_bm = request.getParameter("bmid2");
			// s_gw = request.getParameter("gw2");
		}
		// String s_bmid = request.getParameter("zzbmid");

		int i_tmp = 0;

		if ("2".equals(s_addfs)) {// 添加部门
			s_sql = "select count(*) from v_department where deptname = ? and orgatypeid = ?";
			ps = myJDBC.prepareStatement(s_sql);
			ps.setString(1, s_bm);
			ps.setString(2, s_zzlb);
			rs = ps.executeQuery();
			rs.next();
			i_tmp = rs.getInt(1);
			s_sql = null;
			rs.close();
			rs = null;
			ps.close();
			ps = null;
		}
		if (i_tmp == 0) {
			if (s_addfs.equals("2")) {// 添加部门
				s_sql = "select count(*) from v_duty where deptname = ?  and orgatypeid = ?";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, s_bm);
				ps.setString(2, s_zzlb);
				rs = ps.executeQuery();
				rs.next();
				i_tmp = rs.getInt(1);
				s_sql = null;
				rs.close();
				rs = null;
				ps.close();
				ps = null;

			} else {// 添加岗位
				s_sql = "select count(*) from v_duty where deptname = ? and dutyname = ? and orgatypeid = ?";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, s_bm);
				ps.setString(2, s_gw);
				ps.setString(3, s_zzlb);
				rs = ps.executeQuery();
				rs.next();
				i_tmp = rs.getInt(1);
				s_sql = null;
				rs.close();
				rs = null;
				ps.close();
				ps = null;

			}

			if (i_tmp == 0) {// 没有冲突
				if (parentdeptid.equals("-1")) {
					s_sql = "select nvl(max(deptid),1) from department where parentdeptid = ? and orgatypeid = ? ";
				} else {
					s_sql = "select nvl(max(substr(deptid," + (parentdeptid.length() + 1)
							+ ")),1) from department where parentdeptid = ? ";
				}
				ps = myJDBC.prepareStatement(s_sql);
				ps.setInt(1, Integer.parseInt(parentdeptid));
				if (parentdeptid.equals("-1")) {
					ps.setInt(2, Integer.parseInt(orgatypeid));
				}
				rs = ps.executeQuery();
				rs.next();
				int i_tmp1 = rs.getInt(1);
				s_sql = null;
				rs.close();
				rs = null;
				ps.close();
				ps = null;

				if ("2".equals(s_addfs))// 添加部门
				{
					s_sql = "insert into department(deptid,deptname,orgatypeid,parentdeptid,status) values(?,?,?,?,0)";
					ps = myJDBC.prepareStatement(s_sql);
					if (parentdeptid.equals("-1")) {
						// ps.setInt(1, Integer.parseInt(orgatypeid+(i_tmp1+1)));
						String sql2 = "select count(*) from department where orgatypeid= ? and parentdeptid =-1 ";
						ps2 = myJDBC.prepareStatement(sql2);
						ps2.setString(1, orgatypeid);
						rs = ps2.executeQuery();
						rs.next();
						if (rs.getInt(1) == 0) {
							ps.setInt(1, Integer.parseInt(orgatypeid + (i_tmp1 + 1)));
						} else {
							ps.setInt(1, i_tmp1 + 1);
						}
					} else {
						ps.setInt(1, Integer.parseInt(parentdeptid + String.valueOf((i_tmp1 + 1))));
					}
					ps.setString(2, s_bm);
					ps.setString(3, orgatypeid);
					ps.setInt(4, Integer.parseInt(parentdeptid));
					i_flag = ps.executeUpdate();
					s_sql = null;
					ps.close();
					ps = null;
				}

				s_sql = "select nvl(max(dutyid),1) from duty ";
				ps = myJDBC.prepareStatement(s_sql);
				rs = ps.executeQuery();
				rs.next();
				int i_tmp2 = rs.getInt(1);
				s_sql = null;
				rs.close();
				rs = null;
				ps.close();
				ps = null;

				if ("2".equals(s_addfs)) {// 添加部门
					i_tmp1 = i_tmp1 + 1;
				} else {
					s_sql = "select deptid from department where deptname = ? ";
					ps = myJDBC.prepareStatement(s_sql);
					ps.setString(1, s_bm);
					rs = ps.executeQuery();
					if (rs.next()) {
						i_tmp1 = rs.getInt(1);
					}

					s_sql = null;
					rs.close();
					rs = null;
					ps.close();
					ps = null;

					s_sql = "insert into duty(dutyid,dutyname,deptid) values(?,?,?)";
					ps = myJDBC.prepareStatement(s_sql);
					ps.setInt(1, i_tmp2 + 1);
					ps.setString(2, s_gw);
					ps.setInt(3, i_tmp1);
					i_flag = ps.executeUpdate();
					s_sql = null;
					ps.close();
					ps = null;
				}
			} else {
				i_flag = 0;
			}
		} else {
			i_flag = 0;
		}

		String act = "添加部门";
		if (i_flag > 0) {
			String returnValue = "";
			if (s_addfs.equals("1")) {
				returnValue = "1";
			}
			if (s_addfs.equals("2")) {
				returnValue = "2";
			}
			alertAndReturnValue(out,act + "成功", returnValue);
		} else {
			errAlert(request, out, menu, act + "失败");
		}
		return i_flag > 0;
	}
}
